//厦门大学计算机专业 | 前华为工程师
//专注《零基础学编程系列》  http://lblbc.cn/blog
//包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
//公众号：蓝不蓝编程
Page({
  data: {
    flash: 'off',
    timer: null, // 添加一个定时器变量
    isTimerOn: false,
    arr: [['on', 500],
    ['off', 500],
    ['on', 500],
    ['off', 500],
    ['on', 500],
    ['off', 500],
    ['on', 1500],
    ['off', 500],
    ['on', 1500],
    ['off', 500],
    ['on', 1500],
    ['off', 500]],
    arrIndex: 0
  },
  openTorch: function () {
    this.setData({
      isTimerOn: true
    });
    this.changeLight()
  },
  closeTorch: function () {
    this.setData({
      isTimerOn: false,
      flash: 'off',
      arrIndex: 0
    });
    // 清除定时器
    if (this.data.timer) {
      clearTimeout(this.data.timer);
      this.setData({ timer: null });
    }
  },
  changeLight: function () {
    var _this = this

    var arrIndex = this.data.arrIndex
    var currentData = this.data.arr[arrIndex]
    var flash = currentData[0]
    var timeout = currentData[1]
    // if (flash == 'on') {
    //   flash = 'off'
    // } else {
    //   flash = 'on'
    // }
    this.setData({ flash })
    var timer = setTimeout(() => {
      _this.changeLight()
    }, timeout)
    arrIndex++
    if (arrIndex >= this.data.arr.length - 1) {
      arrIndex = 0
    }
    this.setData({ arrIndex, timer })
  },
})
